<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="添加用户" name="first">
        <el-form ref="userForm" :model="userForm" label-width="120px" :rules="rules" :inline="true">
          <el-form-item label="账号" prop="uaccount">
            <el-input v-model="userForm.uaccount" />
          </el-form-item>
          <el-form-item label="姓名" prop="uname" style="margin-left:15px;">
            <el-input v-model="userForm.uname" />
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-input v-model="userForm.sex" />
          </el-form-item>
          <el-form-item label="GPA" prop="gpa">
            <el-input v-model="userForm.gpa" />
          </el-form-item>
          <el-form-item label="学院" prop="college">
            <el-select v-model="userForm.collegeId" placeholder="请选择学院">
              <el-option :label="item.collegeName" :value="item.id" v-for="item in collegeList"
                :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="专业" prop="major">
            <el-select v-model="userForm.majorId" placeholder="请选择专业">
              <el-option :label="item.majorName" :value="item.id" v-for="item in majorList" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="班级" prop="className">
            <el-select v-model="userForm.classId" placeholder="请选择班级">
              <el-option :label="item.className" :value="item.id" v-for="item in classList" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <!-- 其他可能的表单项 -->
          <br>
          <el-form-item>
            <el-button type="primary" @click="submitForm(3)" style="margin-left:500px;">添加</el-button>
            <el-button @click="resetForm('userForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="添加老师" name="second">
        <el-form ref="userForm" :model="userForm" label-width="120px" :rules="rules" :inline="true">
          <el-form-item label="工号" prop="uaccount">
            <el-input v-model="userForm.uaccount" />
          </el-form-item>
          <el-form-item label="姓名" prop="uname" style="margin-left:15px;">
            <el-input v-model="userForm.uname" />
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-input v-model="userForm.sex" />
          </el-form-item>
          <!-- 其他可能的表单项 -->
          <br>
          <el-form-item>
            <el-button type="primary" @click="submitForm(2)" style="margin-left:500px;">添加</el-button>
            <el-button @click="resetForm('userForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="添加管理员" name="third">
        <el-form ref="userForm" :model="userForm" label-width="120px" :rules="rules" :inline="true">
          <el-form-item label="工号" prop="uaccount">
            <el-input v-model="userForm.uaccount" />
          </el-form-item>
          <el-form-item label="姓名" prop="uname" style="margin-left:15px;">
            <el-input v-model="userForm.uname" />
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-input v-model="userForm.sex" />
          </el-form-item>
          <!-- 其他可能的表单项 -->
          <br>
          <el-form-item>
            <el-button type="primary" @click="submitForm(1)" style="margin-left:500px;">添加</el-button>
            <el-button @click="resetForm('userForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getCollegeAll } from '@/api/college'
import { getMajorAll } from '@/api/major'
import { getClassAll } from '@/api/class'
import { addUser } from '@/api/user'
export default {
  data() {
    return {
      userForm: {
        uaccount: '', // 假设这是自动生成的
        uname: '',
        sex: '',
        gpa: null,
        college: '',
        major: '',
        className: '',
        collegeId: null,
        majorId: null,
        classId: null
      },
      collegeList: [],
      majorList: [],
      classList: [],
      activeName: 'first',
      rules: {
        uaccount: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        uname: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请输入性别', trigger: 'blur' }
        ],
        gpa: [
          { required: true, message: '请输入绩点', trigger: 'blur' }
        ],
        collegeId: [
          { required: true, message: '请选择学院', trigger: 'change' }
        ],
        majorId: [
          { required: true, message: '请选择专业', trigger: 'change' }
        ],
        classId: [
          { required: true, message: '请选择班级', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    handleClick(tab, event) {
      this.userForm = {};
    },
    // 获取list
    getData() {
      getCollegeAll().then(res => {
        this.collegeList = res.data
      })
      getMajorAll().then(res => {
        this.majorList = res.data
      })
      getClassAll().then(res => {
        this.classList = res.data
      })
    },
    submitForm(roleId) {
      // 假设 addNewUser 是添加用户的后端API
      // console.log(this.userForm)
      // 找到选中项的对象
      // 找到选中项的对象

      if (roleId == 3) {
        const selectedCollege = this.collegeList.find(item => item.id === this.userForm.collegeId)
        if (selectedCollege) {
          this.userForm.college = selectedCollege.collegeName // 选中的专业名称
        }
        const selectedMajor = this.majorList.find(item => item.id === this.userForm.majorId)
        if (selectedMajor) {
          this.userForm.major = selectedMajor.majorName // 选中的专业名称
        }
        const selectedClass = this.classList.find(item => item.id === this.userForm.classId)
        if (selectedClass) {
          this.userForm.className = selectedClass.className // 选中的专业名称
        }
        this.userForm.gpa = parseFloat(this.userForm.gpa)
      }
      this.userForm.roleId = roleId;
      addUser(this.userForm).then(response => {
        // 处理添加成功后的逻辑
        if (response.code === 200) {
          this.$message.success('添加成功！')
          this.resetForm()
          this.getData()
        } else {
          this.$message.error('添加失败！')
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.userForm.collegeId = null
      this.userForm.majorId = null
      this.userForm.classId = null
    }
  }
}
</script>
<style scoped>
.line {
  text-align: center;
}
</style>
